import { queryTopPost, queryPost, queryPostCategories, queryPostDetail, queryPostUser, queryPostByUser } from '@/services/api';

export default {
  namespace: 'post',

  state: {
    post:[],
    topPost:[],
    categories:[],
    detail:{},
    tags:[],
    user:{},
    postByUser:[],
    total: '-',
  },

  effects: {
    *fetchTopPost(_, { call, put }) {
      const response = yield call(queryTopPost);
      yield put({
        type: 'saveTopPost',
        payload: Array.isArray(response.data) ? response.data : [],
      });
    },
    *fetchPost({ payload }, { call, put }) {
      const response = yield call(queryPost, payload);
      yield put({
        type: 'savePost',
        payload: Array.isArray(response.data.list) ? response.data.list : [],
      });
    },
    *appendFetchPost({ payload }, { call, put }) {
      const response = yield call(queryPost, payload);
      yield put({
        type: 'appendList',
        payload: Array.isArray(response.data.list) ? response.data.list : [],
      });
    },
    *fetchPostCategories(_, { call, put }) {
      const response = yield call(queryPostCategories);
      yield put({
        type: 'savePostCategories',
        payload: Array.isArray(response.data) ? response.data : [],
      });
    },
    *fetchPostDetail({payload}, { call, put }) {
      const response = yield call(queryPostDetail, payload);
      yield put({
        type: 'savePostDetail',
        payload: response.data ? response.data : {},
      });
    },
    *fetchPostUser({payload}, { call, put }) {
      const response = yield call(queryPostUser, payload);
      yield put({
        type: 'savePostUser',
        payload: response.data ? response.data : {},
      });
    },
    *fetchPostByUser({payload}, { call, put }) {
      const response = yield call(queryPostByUser, payload);
      yield put({
        type: 'savePostByUser',
        payload: response.data,
      });
    },
    *appendFetchPostByUser({ payload }, { call, put }) {
      const response = yield call(queryPostByUser, payload);
      yield put({
        type: 'appendListByUser',
        payload: response.data,
      });
    },
  },

  reducers: {
    saveTopPost(state, action) {
      if(Array.isArray(action.payload)){
        action.payload.forEach(p=>{
          p.user = JSON.parse(p.user);
          p.summaryInfo = p.summaryInfo || p.content;
        });
      }
      return {
        ...state,
        topPost: action.payload,
      };
    },
    savePost(state, action) {
      if(Array.isArray(action.payload)){
        action.payload.forEach(p=>{
          p.user = JSON.parse(p.user);
          p.summaryInfo = p.summaryInfo || p.content;
        });
      }
      return {
        ...state,
        post: action.payload,
      };
    },
    appendList(state, action) {
      if(Array.isArray(action.payload)){
        action.payload.forEach(p=>{
          p.user = JSON.parse(p.user);
          p.summaryInfo = p.summaryInfo || p.content;
        });
      }
      return {
        ...state,
        post: state.post.concat(action.payload),
      };
    },
    savePostCategories(state, action) {
      return {
        ...state,
        categories: action.payload,
      };
    },
    savePostDetail(state, action) {
      const d =  {...action.payload};
      if(d){
        d.user = JSON.parse(d.user);
        d.tags = JSON.parse(d.tags);
        d.avatar = d.user.avatarLarge;
        d.owner = d.user.username;
        d.content = d.detail ? d.detail.content : d.content;
        d.href = d.originalUrl;
      }
      return {
        ...state,
        detail: d,
        tags: d.tags
      };
    },
    savePostUser(state, action) {
      return {
        ...state,
        user: action.payload
      };
    },
    savePostByUser(state, action) {
      if(Array.isArray(action.payload.list)){
        action.payload.list.forEach(d=>{
          d.tags = JSON.parse(d.tags);
          d.user = JSON.parse(d.user);
          d.avatar = d.user.avatarLarge;
          d.owner = d.user.username;
          d.content = d.detail ? d.detail.content : d.content;
          d.href = d.originalUrl;
        });
      }
      return {
        ...state,
        postByUser: action.payload.list,
        total: action.payload.total
      };
    },
    appendListByUser(state, action) {
      if(Array.isArray(action.payload.list)){
        action.payload.list.forEach(d=>{
          d.tags = JSON.parse(d.tags);
          d.user = JSON.parse(d.user);
          d.avatar = d.user.avatarLarge;
          d.owner = d.user.username;
          d.content = d.detail ? d.detail.content : d.content;
          d.href = d.originalUrl;
        });
      }
      return {
        ...state,
        postByUser: state.postByUser.concat(action.payload.list),
        total: action.payload.total
      };
    },
  },
};
